package com.bjc.base.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.lang3.StringUtils;
/**
 * 文件工具类
 *
 * @author 许自辉
 * @date 2017年10月17日
 */
public class FileUtil {
	/**
	 * 读文件得到byte[]
	 * 
	 * @param filename
	 * @return
	 * @throws IOException
	 */
	public static byte[] getContent(String filename) throws IOException {
		File f = new File(filename);
		if (!f.exists()) {
			throw new FileNotFoundException(filename);
		}
		ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
		BufferedInputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(f));
			int bufSize = 1024;
			byte[] buffer = new byte[bufSize];
			int len = 0;
			while (-1 != (len = in.read(buffer, 0, bufSize))) {
				bos.write(buffer, 0, len);
			}
			return bos.toByteArray();
		} catch (IOException e) {
			throw e;
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				throw e;
			}
			bos.close();
		}
	}

	/**
	 * 读文件得到byte[]
	 * 
	 * @param filename
	 * @return
	 * @throws IOException
	 */
	public static byte[] getContentByFile(File file) throws IOException {
		if (!file.exists()) {
		}
		ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
		BufferedInputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(file));
			int bufSize = 1024;
			byte[] buffer = new byte[bufSize];
			int len = 0;
			while (-1 != (len = in.read(buffer, 0, bufSize))) {
				bos.write(buffer, 0, len);
			}
			return bos.toByteArray();
		} catch (IOException e) {
			throw e;
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				throw e;
			}
			bos.close();
		}
	}

	/**
	 * 根据文件路径创建文件
	 * 
	 * @author 许自辉
	 * @date 2017年10月17日
	 * @param destDirName
	 *            文件路径
	 * @return
	 */
	public static boolean createDir(String destDirName) {
		if (destDirName.indexOf(".") > 0) {
			if (destDirName.contains("/")) {
				destDirName = destDirName.substring(0, destDirName.lastIndexOf("/"));
			}
		}
		File dir = new File(destDirName);
		// 判断目录是否存在
		if (dir.exists()) {
			return false;
		}
		// 结尾是否以"/"结束
		if (!destDirName.endsWith(File.separator)) {
			destDirName = destDirName + File.separator;
		}
		// 创建目标目录
		if (dir.mkdirs()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 根据创建文件夹并创建文件
	 * 
	 * @author 许自辉
	 * @date 2017年10月10日
	 * @param filePath
	 *            文件路径
	 * @param dates
	 *            写入的字符流
	 * @return
	 * @throws Exception
	 */
	public static boolean writeFile(String filePath, byte[] dates) throws Exception {
		createDir(filePath);
		File file = new File(filePath);
		if (!file.exists()) {
			file.createNewFile();
			File outputFiles = new File(filePath);
			FileOutputStream fs = null;
			try {
				fs = new FileOutputStream(outputFiles);
				fs.write(dates);
			} catch (Exception e) {
				throw e;
			} finally {
				try {
					if (fs != null) {
						fs.close();
					}
				} catch (IOException e) {
					throw e;
				}
			}
		}
		return true;
	}

	/**
	 * 根据创建文件夹并创建文件
	 * 
	 * @author 许自辉
	 * @date 2017年10月10日
	 * @param filePath
	 *            文件路径
	 * @param dates
	 *            写入的字符流
	 * @return
	 * @throws Exception
	 */
	public static boolean writeFile(String filePath, InputStream inputStream) throws Exception {
		if (StringUtils.isBlank(filePath) || null == inputStream) {
			return false;
		}
		byte[] data = new byte[1024];
		int len = 0;
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(filePath);
			while ((len = inputStream.read(data)) != -1) {
				fileOutputStream.write(data, 0, len);
			}
		} catch (IOException e) {
			throw e;
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					throw e;
				}
			}
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					throw e;
				}
			}

		}
		return true;
	}

}
